package com.lbn.jfqsc.app.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.baseframe.utils.L;
import com.lbn.baseframe.utils.SPUtils;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.login.register.RegisterActivity;
import com.lbn.jfqsc.business.bean.User;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoaginView {

    @BindView(R.id.user_mobile)
    TextInputEditText userMobile;
    @BindView(R.id.user_password)
    TextInputEditText userPassword;
    @BindView(R.id.login)
    Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));
        appbar.setTitle("登录");
        appbar.openBack();
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }


//    @OnClick(R.id.login)
//    public void onClick() {
//        mPresenter.login(userMobile.getText().toString(), userPassword.getText().toString());
//    }

    @Override
    public void onSucceed(User user) {
        user.setPassword(userPassword.getText().toString());
        App.setUser(user);
        String userJson = new Gson().toJson(user);
        L.e("saveUser:" + userJson);
        SPUtils.put(this, "USER", userJson);
        finish();
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }

    @OnClick({R.id.login, R.id.register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login:
                mPresenter.login(userMobile.getText().toString(), userPassword.getText().toString());
                break;
            case R.id.register:
                RegisterActivity.startActivity(this);
                break;
        }
    }

//    @OnClick(R.id.register)
//    public void onClick() {
//    }
}
